package org.example.utils;

import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.io.json.JettisonMappedXmlDriver;
import com.thoughtworks.xstream.io.xml.DomDriver;
import com.thoughtworks.xstream.io.xml.StaxDriver;
import org.apache.commons.lang3.StringUtils;
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class XStreamUtils {


    /**
     * xml转换成javabean
     *
     * @return
     * @throws IOException
     */
    public static XStream domDriver() throws IOException {
        XStream xStream = new XStream(new DomDriver("GBK"));

        // 开启自动扫描注解
        xStream.autodetectAnnotations(true);

        return xStream;

    }

    /**
     * xml转换成javabean 无换行
     *
     * @return
     */
    public static XStream staxDriver() {
        XStream xStream = new XStream(new StaxDriver());

        // 开启自动扫描注解
        xStream.autodetectAnnotations(true);

        return xStream;

    }

    /**
     * java to json
     *
     * @return
     */
    public static XStream jettisonMappedXmlDriver() {
        XStream xStream = new XStream(new JettisonMappedXmlDriver());

        // 开启自动扫描注解
        xStream.autodetectAnnotations(true);

        return xStream;

    }

    /**
     * xml 转map
     *
     * @param xml
     * @return
     * @throws Exception
     */
    public static Map<String, Object> parseXml(String xml) throws Exception {

        System.out.println("解析目标：" + xml);

        Map<String, Object> map = new HashMap<>();
        Document document = DocumentHelper.parseText(xml);
        Element root = document.getRootElement();
        List<Element> elementList = root.elements();

        for (Element e : elementList){
            map.put(e.getName(), e.getText());
        }
        return map;
    }

    /**
     * 转换java对象
     *
     * @param xml
     * @param object
     * @return
     * @throws IOException
     */
    public static Object toBean(String xml, Object object) throws IOException {
        XStream xStream = XStreamUtils.domDriver();
        xStream.processAnnotations(object.getClass());
        return xStream.fromXML(xml);
    }

    /**
     * 获取xml
     *
     * @param object
     * @return
     * @throws IOException
     */
    public static String toXml(Object object) throws IOException {

        String xml_version_en = "<?xml version=\"1.0\" encoding=\"GBK\"?>\n";

        System.out.println(xml_version_en + domDriver().toXML(object));

        return xml_version_en + domDriver().toXML(object);

    }

    /**
     * 可编码格式
     *
     * @param object
     * @param en
     * @return
     * @throws IOException
     */
    public static String toXml(Object object, String en) throws IOException {

        if (StringUtils.isBlank(en)) {
            en = "GBK";
        }

        String xml_version_en = "<?xml version=\"1.0\" encoding=\"" + en + "\"?>\n";

        return xml_version_en + domDriver().toXML(object);

    }

    public static void main(String[] args) throws Exception {
        String xml = "<?xml version=\"1.0\" encoding=\"GBK\"?><stream><hostNo>B0002011</hostNo><status>AAAAAAA</status><statusText>交易成功</statusText><subAccNm>天筑科技共享平台</subAccNm><subAccNo>3110730020353007636</subAccNo></stream>";

        System.out.println(parseXml(xml));
    }

}
